﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using Pheonix.Fashionzoomer.DAL;

public partial class admin_shopcategorylist : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - Shop List [ Admin ]";
        deleteAllButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");

        if (!IsPostBack)
        {
            try
            {
                if (Request["id"] != null)
                {
                    int id = 0;
                    if (int.TryParse(Request["id"], out id))
                    {
                        ViewState["id"] = id;
                        LoadData();
                    }
                }
            }
            catch (Exception ex)
            {
                uccResponseMessage.SetFailMessage("There was a problem in loading the page");
            }
        }
    }

    private void LoadData()
    {
        int id = 0;
        if (ViewState["id"] != null)
            id = (int)ViewState["id"];

        if (id > 0)
        {
            DataTable shopCategories = CustomCommand.GetShopCategoryByShopId(id);
            shopCategoryList.DataSource = shopCategories;
            shopCategoryList.DataBind();
        }

        ddlCategory.Items.Clear();
        ddlCategory.DataSource = Category.GetAllCategory();
        ddlCategory.DataTextField = "CategoryName";
        ddlCategory.DataValueField = "Id";
        ddlCategory.Items.Insert(0, new ListItem("Select", "-1"));
        ddlCategory.DataBind();
    }

    protected void shopCategoryList_RowCommand(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "deleteCommand")
        {
            try
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                int shopCategoryId = (int)shopCategoryList.DataKeys[rowIndex].Value;

                ShopCategory.DeleteShopCategory(shopCategoryId);

                LoadData();
                uccResponseMessage.SetSuccessMessage("Deleted 1 items");
            }
            catch
            {
                uccResponseMessage.SetFailMessage("Failed to delete items");
            }
        }
    }

    protected void shopCategoryList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox selectAll = (CheckBox)e.Row.FindControl("selectAll");
            selectAll.Attributes.Add("onclick", "SelectAll('" + shopCategoryList.ClientID + "','" + selectAll.ClientID + "')");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int32 id = (Int32)shopCategoryList.DataKeys[e.Row.RowIndex].Value;

            ImageButton deleteButton = (ImageButton)e.Row.FindControl("deleteButton");
            deleteButton.AlternateText = "delete";
            deleteButton.CommandArgument = e.Row.RowIndex.ToString();
            deleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
        }
    }

    protected void addNewButton_Click(object sender, ImageClickEventArgs e)
    {
        if (ViewState["id"] != null)
        {
            int shopId = (int)ViewState["id"];
            if (shopId >0)
            {
                int categoryId = 0;
                if (int.TryParse(ddlCategory.SelectedValue, out categoryId))
                {
                    int newShopCategoryId = ShopCategory.CreateNewShopCategory(shopId, categoryId, 0,
                        DateTime.Now, DateTime.Now, 0);
                    if (newShopCategoryId > 0)
                    {
                        LoadData();
                        uccResponseMessage.SetSuccessMessage("Shop Category Successfuly Created");
                    }
                    else
                        uccResponseMessage.SetFailMessage("Shop category creation failed");
                }
                else
                    uccResponseMessage.SetFailMessage("Please Select a category");
            }
            else
                uccResponseMessage.SetFailMessage("Invalid shop");
        }
    }

    protected void deleteAllButton_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GridViewRowCollection shopCategoryListRows = shopCategoryList.Rows;
            List<int> idToDelete = new List<int>();
            
            foreach (GridViewRow row in shopCategoryListRows)
            {
                CheckBox rowSelection = (CheckBox)row.FindControl("rowSelect");

                if (rowSelection.Checked)
                {
                    idToDelete.Add((int)shopCategoryList.DataKeys[row.RowIndex].Value);
                }
            }

            if (ShopCategory.DeleteShopCategoryList(idToDelete))
            {
                LoadData();
                uccResponseMessage.SetSuccessMessage("Deleted " + idToDelete.Count.ToString() + " items");
            }
            else
            {
                uccResponseMessage.SetFailMessage("Failed to delete items");
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to delete items");
        }
    }
}
